package cn.zaichi.edu.excelcommon.handler;


import cn.zaichi.edu.excelcommon.constants.ProcessContext;
import cn.zaichi.edu.excelcommon.constants.ProcessType;
import cn.zaichi.edu.excelcommon.process.ExcelImportDataInfo;
import cn.zaichi.edu.excelcommon.process.ProcessEntity;
import cn.zaichi.edu.excelcommon.process.ProcessingExcel;

public class ImportExcelHandler implements BaseExcelHandler {
    /**
     * 导入excel 概况
     */
    private ExcelImportDataInfo importDataInfo;

    public ImportExcelHandler(ExcelImportDataInfo importDataInfo) {
        this.importDataInfo = importDataInfo;
    }


    public void setImportDataInfo(ExcelImportDataInfo importDataInfo) {
        this.importDataInfo = importDataInfo;
    }


    /**
     * 读取单元格成功
     *
     * @param sheet     sheet下标
     * @param row       行下标
     * @param cell      列下标
     * @param cellValue 单元格内容
     */
    @Override
    public void onSuccessCell(int sheet, int row, int cell, String cellValue,ProcessingExcel processing) {
        /*double allProcess = ProcessContext.get(importDataInfo.getUuid()).getAllProcess();
        double sheetProcess = ProcessContext.get(importDataInfo.getUuid()).getSheetProcess();
        ProcessContext.put(importDataInfo.getUuid(), new ProcessEntity(sheet, row, cell, "", "", allProcess, sheetProcess));
        processing.callback(importDataInfo.getUuid(),ProcessContext.get(importDataInfo.getUuid()),ProcessType.onSuccessCell);*/
    }

    /**
     * 读取单元格失败
     *
     * @param sheet         sheet下标
     * @param row           行下标
     * @param cell          列下标
     * @param errorMsg 失败原因
     */
    @Override
    public void onErrorCell(int sheet, int row, int cell, String errorMsg,int errorCode, ProcessingExcel processing) {
        /*double allProcess = ProcessContext.get(importDataInfo.getUuid()).getAllProcess();
        double sheetProcess = ProcessContext.get(importDataInfo.getUuid()).getSheetProcess();
        ProcessContext.put(importDataInfo.getUuid(), new ProcessEntity(sheet, row+1, cell, errorMsg, "", allProcess, sheetProcess,errorCode));
        processing.callback(importDataInfo.getUuid(),ProcessContext.get(importDataInfo.getUuid()),ProcessType.onErrorCell);*/
    }

    /**
     * 当前行读取完成
     *
     * @param sheet sheet下标
     * @param row   行下标
     */
    @Override
    public void onFinishRow(int sheet, int row,ProcessingExcel processing) {
        double allProcess = ProcessContext.get(importDataInfo.getUuid()).getAllProcess();
        double sheetProcess = 1.00 *row / importDataInfo.getRowCount() * 100;
        ProcessContext.put(importDataInfo.getUuid(), new ProcessEntity(sheet, row, -1,"共有"+importDataInfo.getRowCount()+"行，已读取"+row+"行..", "", allProcess, sheetProcess));

        processing.callback(importDataInfo.getUuid(),ProcessContext.get(importDataInfo.getUuid()),ProcessType.onFinishRow);
    }



    /**
     * 当前sheet读取完成
     *
     * @param sheet sheet下标
     */
    @Override
    public void onFinishSheet(int sheet,ProcessingExcel processing) {
        int allProcess = sheet / importDataInfo.getSheetCount() * 100;
        ProcessContext.put(importDataInfo.getUuid(), new ProcessEntity(sheet, -1, -1, "共有"+importDataInfo.getSheetCount() + "个sheet，当前第" +(sheet+1)+ "个sheet，读取完成", "", allProcess, 0));
        if(sheet+1<importDataInfo.getSheetCount()){
            ProcessContext.put(importDataInfo.getUuid(), new ProcessEntity(sheet, -1, -1, "共有"+importDataInfo.getSheetCount() + "个sheet，已读取第" +(sheet+1)+ "个sheet..", "", allProcess, 0));
        }

        processing.callback(importDataInfo.getUuid(),ProcessContext.get(importDataInfo.getUuid()),ProcessType.onFinishSheet);
    }

    /**
     * 当前EXCEL 读取完成
     */
    @Override
    public void onFinishExcel(ProcessingExcel processing) {
        ProcessContext.put(importDataInfo.getUuid(), new ProcessEntity(-1, -1, -1, "验证完成", "", 100, 100));
        processing.callback(importDataInfo.getUuid(),ProcessContext.get(importDataInfo.getUuid()),ProcessType.onFinishExcel);
    }

    /**
     * 导入失败
     */
    @Override
    public void onError(String errorMsg,int errorCode,ProcessingExcel processing) {
        processing.callback(importDataInfo.getUuid(),ProcessContext.get(importDataInfo.getUuid()),ProcessType.onError);
    }

}
